package com.LeeCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 连续差相同的数字
 */

public class Code967 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Code967().numsSameConsecDiff(2, 2)));
    }

    public int[] numsSameConsecDiff(int n, int k) {
        List<Integer> ans = new ArrayList<>();
        for (int i = 1; i <= 9; i++) {
            backtrace(1, i, ans, n, k);
        }
        return ans.stream().mapToInt(i -> i).toArray();
    }

    private void backtrace(int index, int num, List<Integer> ans, int n, int k) {
        if (index == n) {
            ans.add(num);
            return;
        }
        int prev = num % 10;
        // 变大
        if (prev + k <= 9) {
            backtrace(index + 1, num * 10 + prev + k, ans, n, k);
        }
        // 变小
        if (prev - k >= 0 && k != 0) {
            backtrace(index + 1, num * 10 + prev - k, ans, n, k);
        }
    }
}
